<?php

require('../coupon/tcpdf.php');
require_once('../coupon/pdf/fpdi.php');
require_once('../coupon/config/lang/eng.php');
require('../wp-load.php');

function contest_format_date($input) {
    $d = substr($input, 0, 16);    
    $d = str_replace("-",".", $d);
    
    $darrs = explode(" ", $d);
    $date = $darrs[0];
    $time = $darrs[1];
    $dateArrs = explode(".", $date);
    $dateStr = $dateArrs[2] . "." . $dateArrs[1] . "." . $dateArrs[0];
    return $dateStr . " " . $time;
}

function contest_display_as_number($num, $checknum = 1) {
    if(!$checknum) {
        return "0";
    }
    if($num){
        return $num;
    } else {
        return "0";
    }
}

global $current_user;
// get data from database
$contestID = $_GET['contest_id'];
$smtUserID = $_GET['smt_user_id'];
$result = $wpdb->get_results("
    select * from
    (
    select scd.*, ld.user_like_id, count(*) as num_of_like from 
            (select * from st_contest_data where contest_id = {$contestID}) scd
                left join
            (select * from st_contest_user_like_data where smt_user_id = {$smtUserID}) ld
                on scd.data_id = ld.data_id
            group by scd.data_id
    ) tb1 left join st_contest_user lu
    on tb1.user_id = lu.user_id
    ");
$name = "All contests";
$contestDetail = $wpdb->get_row("select * from  st_contest_contest where contest_id = {$_GET['contest_id']}");
$name = $contestDetail->name;


// export csv
$exportFileName = "detailed-statistics-" . date_i18n('d-m-Y') . "_" . time();
class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;
    private $pdf_name;
    private $name;

    function __construct($name, $orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false) {
        $this->name = $name;
        parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache);
    }

    /**
     * include a background template for every page
     */
    function Header() {
        global $current_user;
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile('../coupon/example.pdf');
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);
        $this->setFont('freeserif', '', 7);
        $this->writeHTMLCell(27, 0, 187, 9, '<p align="right">' . $this->PageNo() . '/' . $this->getAliasNbPages() . '</p>');
        $this->writeHTMLCell(22, 0, 184, 12, '<p align="right">' . date_i18n('d.m.Y H:i:s') . '</p>');
        $this->setFont('freeserif', 'B', 12);
        $this->writeHTMLCell(0, 0, 0, 10, '<p align="center">Statistics for ' . $this->name . '</p>');
        $this->setFont('freeserif', 'B', 8);
        $this->SetXY(5, 31);
        $html = '<table border="0" cellpadding="2" cellspacing="0" width="570">';
        $html .= '<tr style="border-bottom:solid 1px #000000;"><td width="10%"  align="center">ID</td><td width="30%">Name</td><td width="30%" align="center">Date/time for the contribution</td><td width="30%" align="center">Number of votes</td></tr>';
        $html .= '</table>';
        $this->writeHTML($html);
    }

    function Footer() {

    }
}

$pdf = new PDF($name);
$pdf->SetMargins(PDF_MARGIN_LEFT, 37, PDF_MARGIN_RIGHT);
$pdf->addPage();

// End Name 
$pdf->SetXY(5, 37);
$pdf->setFont('freeserif', '', 8);
$html = '<table border="0" cellpadding="2" cellspacing="0" width="570">';
$stt = 1;
foreach ($result as $row) {
    $birthday = explode('-', $row->birthday);
    $birthday = $birthday[2] . '.' . $birthday[1] . '.' . $birthday[0];
    $background = '';
    if (($stt % 2) == 0)
        $background = 'style="background-color:#bfe1ff"';        
    $html .= '<tr ' . $background . '><td width="10%"  align="center">' . $row->data_id . '</td><td width="30%">' . $row->name . '</td><td width="30%" align="center">' . contest_format_date($row->created_time) . '</td><td width="30%" align="center">' . contest_display_as_number($row->num_of_like, $row->user_like_id) . '</td></tr>';
    $stt++;
}
$html .='<tr><td colspan="7"><br style="height:1px;" /><hr style="height:0.7px;font-weight:bold;"></td></tr>';
$html .= '</table>';
$pdf->writeHTML($html);
$date = date_i18n('d-m-Y-h-i-s');
$pdf->Output('data/' . $exportFileName . '.pdf', 'F');
echo "<script>window.location = '" . get_bloginfo('url') . "/contest/data/{$exportFileName}.pdf';
// window.close();
</script>";
?>